package com.typy.www.tpay.ui.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.vm.ActivicationVm;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.LocalDisplay;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by jiajie on 2018/1/24.
 */

public class BuyInDialog extends Dialog {

    protected Context mContext;

    private View mView;
    private LinearLayout mLlContent;
    private PercentRelativeLayout mRlContent;
    private LinearLayout ll_save;
    private ImageView mIvDelete;
    private TextView mTvTip;
    private ViewHolder holder;

    private OnDismantlingListener listener;

    public BuyInDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        this.mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.item_buydailog, null);
        holder=new ViewHolder(mView);

//        //图片的宽高
        int picW = holder.llHolder.getWidth();
        int picH = holder.llHolder.getHeight();


        int h = LocalDisplay.SCREEN_HEIGHT_PIXELS;
        int w = LocalDisplay.SCREEN_WIDTH_PIXELS;

//        w = (w * 4) / 5;
//        h = (h * 5) / 5;
        this.addContentView(mView,
                new LinearLayout.LayoutParams(
                        w,
                        h));//设置大小
    }

    public OnDismantlingListener getListener() {
        return listener;
    }

    public void setListener(OnDismantlingListener listener) {
        this.listener = listener;
    }

    /**
     */
    public void updataView(ActivicationVm vm,int classType) {
        String name=vm.getFdNickName();
        if(name.length()>5){
            name=name.substring(0,5)+"...";
        }
        if(classType== ClassHoldUtil.BUYINCENTER) {
            UpdataUiUtil.getDefaul().setText("确定购买",holder.tvQueding)
                    .setText("取消购买",holder.tvQuxiao)
                    .setText(vm.getFdAmount(),holder.etChushoushuliang)
                    .setText(vm.getFdMoney(),holder.etShishoujine)
                    .setText(vm.getFdBankName(),holder.etChushoujiage)
                    .setText("您确定要向[" + name + "]购买", holder.tvBuytitle);

        }else {
            UpdataUiUtil.getDefaul().setText("确定出售",holder.tvQueding)
                    .setText("取消出售",holder.tvQuxiao)
                    .setText(vm.getFdAmount(),holder.etChushoushuliang)
                    .setText(vm.getFdMoney(),holder.etShishoujine)
                    .setText(vm.getFdBankName(),holder.etChushoujiage)
                    .setText("您确定要向[" + name + "]出售", holder.tvBuytitle);
        }
        holder.llHolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               dismiss();
            }
        });
        holder.tvQuxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        holder.tvQueding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onDismantlingRespon(1);
                }
            }
        });
    }

    /**
     * 拆红包
     */
    public interface OnDismantlingListener {
        public void onDismantlingRespon(int type);
    }

    static class ViewHolder {
        @BindView(R.id.tv_buytitle)
        TextView tvBuytitle;
        @BindView(R.id.tv_chushoushuliang)
        TextView tvChushoushuliang;
        @BindView(R.id.et_chushoushuliang)
        AutoCompleteTextView etChushoushuliang;
        @BindView(R.id.ll_xiane)
        PercentLinearLayout llXiane;
        @BindView(R.id.tv_shishoujine)
        TextView tvShishoujine;
        @BindView(R.id.et_shishoujine)
        AutoCompleteTextView etShishoujine;
        @BindView(R.id.ll_shishoujine)
        PercentLinearLayout llShishoujine;
        @BindView(R.id.tv_zhifufangshi)
        TextView tvZhifufangshi;
        @BindView(R.id.et_chushoujiage)
        AutoCompleteTextView etChushoujiage;
        @BindView(R.id.ll_zhifufangshi)
        PercentLinearLayout llZhifufangshi;
        @BindView(R.id.tv_quxiao)
        TextView tvQuxiao;
        @BindView(R.id.tv_queding)
        TextView tvQueding;
        @BindView(R.id.ll_sell)
        PercentLinearLayout llSell;
        @BindView(R.id.ll_holder)
        LinearLayout llHolder;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
